package ball;

import javax.swing.*;
import java.awt.*;

public class BallGameXYMove extends JFrame {

    Image ball = Toolkit.getDefaultToolkit().getImage("images/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("images/desk.png");
    boolean  right = true;   //控制小乔方向
    double x=100;
    double y=100; // 小球的横纵坐标

    double degree = 60 ; //按60度角移动  => 60.0 度的正弦值为 1.0471975511965976

    //    double  radians = 3.14/3; 弧度的意思


    //画窗口的方法
    public void paint(Graphics g){
        g.drawImage(desk,0,0,null);
        g.drawImage(ball,(int)x,(int)y,null);

        // 表示按照某个弧度进行轨迹行走 Math.cos(Math.toRadians(degree))
        x = x + 10*Math.cos(Math.toRadians(degree)); // 将角度转换为弧度 的公式 => 弧度＝(角度/180) *PI   => 即  弧度 = (60/180)*3.14 也就是 radians = 3.14/3
        y = y + 10*Math.sin(Math.toRadians(degree));

        System.out.println(x+":"+degree);

        if(x<40 || x> 856-40-40){ //碰到左右两边，跟y轴对称
            System.out.println("--------------------------------------");
            degree =180 - degree;
        }
        if(y>500-40-50 || y< 40+40){ //碰到上下边界，角度变换
            degree = - degree;
        }
    }

    // 窗口加载
    void launchFragme(){
        setSize(856,500);
        setLocation(50,50);
        setVisible(true);

        while (true){
            repaint();
            try {
                Thread.sleep(40);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args){
        BallGameXYMove ballGame = new BallGameXYMove();
        ballGame.launchFragme();

    }
}
